{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92f4bd81-b18e-4273-86af-7339c2aa3740",
   "metadata": {
    "panel-layout": {
     "height": 0,
     "visible": true,
     "width": 100
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/ 9/ 10/ "
     ]
    }
   ],
   "source": [
    "# 基础输出\n",
    "lis = [1, 9, 10]\n",
    "for i in lis:\n",
    "    print(i, end='/ ') # print 可配置结束符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3f9ae2c6-506f-4033-bc38-8e4293844613",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 比较运算符\n",
    "print(1 != 2)\n",
    "x = 1\n",
    "print(x is not None) # is not 用于能直接从当前语句推断出两边类型时, 会提示语法告警, 比如 x is not 2 会提示使用 !="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ed74bbc9-e9ea-4433-9f4f-46015c6ff10c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.961\n",
      "1.0\n",
      "3.8440000000000003\n",
      "(3+4j)\n",
      "(3-4j)\n",
      "(3-4j)\n",
      "(1.0, 3.8440000000000003)\n",
      "8\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# 算术运算符\n",
    "print(7.844 / 4)\n",
    "print(7.844 // 4)  # 返回除法的整数部分 \n",
    "print(7.844 % 4)  # 返回余数部分\n",
    "print(complex(3, 4))  # 定义复数\n",
    "print(complex(3, 4).conjugate())  # 定义共轭复数, 基于原点的向量垂直\n",
    "print((3+4j).conjugate())\n",
    "print(divmod(7.844, 4))  # 返回整数和余数部分的二元组\n",
    "print(2 ** 3 )\n",
    "print(pow(2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e377f2f2-9fdf-4fdb-b45e-c457578e400c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nimport math\\n\\nA = float(input('输入A: '))\\nB = float(input('输入B: '))\\nC = float(input('输入C: '))\\n\\ndelta = B ** 2 - 4 * A * C\\nx1 = (B + math.sqrt(delta)) / (-2 * A)\\nx2 = (B - math.sqrt(delta)) / (-2 * A)\\n\\nprint(x1, x2)\\n\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解一元二次方程 Ax^2+Bx+C=0\n",
    "'''\n",
    "import math\n",
    "\n",
    "A = float(input('输入A: '))\n",
    "B = float(input('输入B: '))\n",
    "C = float(input('输入C: '))\n",
    "\n",
    "delta = B ** 2 - 4 * A * C\n",
    "x1 = (B + math.sqrt(delta)) / (-2 * A)\n",
    "x2 = (B - math.sqrt(delta)) / (-2 * A)\n",
    "\n",
    "print(x1, x2)\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c47b2895-f299-4dbc-8403-9bb049bbc84e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#a = input('输入a: ')\n",
    "#type(a) is str  # 数据类型判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d63de5ae-2f29-4d32-8908-91b786277b62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "His name is Kitt, 50 years old.\n"
     ]
    }
   ],
   "source": [
    "# 字符串占位符格式化输出\n",
    "print('His name is %s, %d years old.' % ('Kitt', 50))  # %d 整数, %s 字符串, %f 浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c47b85e6-a250-4b65-a2cf-63e5e5c30bc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[5, 8, 11, 14, 17, 20, 23, 26, 29]\n",
      "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
     ]
    }
   ],
   "source": [
    "#　序列，列表，元组\n",
    "print(list(range(10)))  # range(n) 创建 [0, n-1)的序列\n",
    "print(list(range(5, 30, 3)))  # range(5, 30, 3)创建 5 -> 30 以3为步进的序列\n",
    "print(list(range(2, 15)))  # range(2, n) 创建 [2, n-1)的序列\n",
    "\n",
    "a =  range(10)\n",
    "print(list(a))  # 转列表\n",
    "print(tuple(a)) # 转元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "59bbdfac-1884-4ab9-8d49-d766af75a4ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exp: division by zero\n"
     ]
    }
   ],
   "source": [
    "# 异常处理, 后三个处理都是可选, 存在时必须按序, else存在必须有except\n",
    "'''\n",
    "try:\n",
    "    {do something}\n",
    "except A:\n",
    "    {do something}\n",
    "except B as exp:\n",
    "    {do something}\n",
    "else:\n",
    "    {do something}\n",
    "finally:\n",
    "    {do something}\n",
    "'''\n",
    "\n",
    "try:\n",
    "    print(1/0)\n",
    "except Exception as exp:\n",
    "    print('exp: %s' % exp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aefb38bc-4405-4ee8-94f3-cdaa330aa928",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有参数\n"
     ]
    }
   ],
   "source": [
    "# 参数结构\n",
    "\n",
    "# 无参\n",
    "def fun0():\n",
    "    print('没有参数')\n",
    "\n",
    "fun0()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ce09d790-efaf-4efd-90e1-0e39c5a6ce7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "固定参数 45\n"
     ]
    }
   ],
   "source": [
    "# 固定参数\n",
    "def fun1(x):\n",
    "    print('固定参数 %s' % x)\n",
    "\n",
    "fun1(45)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bd58f891-2442-4952-8ce3-a8aa404d966e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15 is not 100? or ok\n",
      "15 is not 100? or 40\n",
      "90 is not 88? or Look\n"
     ]
    }
   ],
   "source": [
    "# 默认参数\n",
    "def fun2(x, y=100, z='ok'):\n",
    "    print('%s is not %d? or %s' % (x, y, z))\n",
    "\n",
    "fun2('15')\n",
    "fun2('15', z='40') # 默认参数的函数在使用时, 严格要求按序传参, 否者标明参数名\n",
    "fun2(z='Look', x='90', y=88) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6eb48587-8d12-44cb-ae90-bedc2a38ea6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 有很多别名: py, P, gdL, \n",
      "----\n"
     ]
    }
   ],
   "source": [
    "# 未知参数个数\n",
    "def fun3(name, *param):\n",
    "    print('%s 有很多别名' % name, end=': ')\n",
    "    for i in param:\n",
    "        print(i, end=', ')\n",
    "    print('\\n----')\n",
    "\n",
    "fun3('Python', 'py', 'P', 'gdL')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e54bfe16-1add-40a6-af3d-647eb9dde158",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "java L1\n",
      "c low\n",
      "age 77\n"
     ]
    }
   ],
   "source": [
    "# 带键参数\n",
    "def fun4(**k):\n",
    "    print(type(k))\n",
    "    for i in k:\n",
    "        print(i, k[i])\n",
    "        \n",
    "fun4(java='L1', c='low', age=77)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "91b93bd1-26dd-4fef-9d9c-436332ff82b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write ppt as: \n",
      "固定参数 你呐\n"
     ]
    }
   ],
   "source": [
    "# 回调函数, 函数的参数是一个函数\n",
    "def fun5(fun, ppt):\n",
    "    print('write ppt as: ')\n",
    "    fun(ppt)\n",
    "\n",
    "fun5(fun1, '你呐')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "caef112c-2c21-4a38-a2d3-19bfb5817e85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fun6(1)=1\n",
      "fun6(2)=1\n",
      "fun6(3)=2\n",
      "fun6(4)=3\n",
      "fun6(5)=5\n",
      "fun6(6)=8\n",
      "fun6(7)=13\n",
      "fun6(8)=21\n",
      "fun6(9)=34\n"
     ]
    }
   ],
   "source": [
    "# 递归函数, 耗内存(递归时栈数量n, 迭代时栈数量为1), 能迭代的别递归\n",
    "def fun6(n):\n",
    "    if n <= 2:\n",
    "        return 1\n",
    "    else:\n",
    "        return fun6(n - 1) + fun6(n - 2)\n",
    "\n",
    "for i in range(1, 10):\n",
    "    print('fun6(%d)=%d' % (i, fun6(i)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "751dbbb9-7f2d-4862-92f9-d99995da81fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "param 胡萝卜 length: 3\n",
      "param 青菜 length: 2\n"
     ]
    }
   ],
   "source": [
    "# 嵌套函数\n",
    "def fun7():\n",
    "    def funi(name):\n",
    "        print('param %s length: %d' %(name, len(name)))\n",
    "    \n",
    "    funi('胡萝卜')\n",
    "    funi('青菜')\n",
    "\n",
    "fun7()\n",
    "# 递归: 第n步的值依赖于第n-1步的值, 会在函数内部调用自己\n",
    "# 嵌套: 在函数内部再定义函数, 常用于封装重复代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9d95a69b-1cd4-4fe0-9591-28228ff54357",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小强 say: ok\n",
      "current values: [11, 12]\n"
     ]
    }
   ],
   "source": [
    "# 闭包, 返回结果是一个函数(回调: 参数是函数)\n",
    "def say(word):\n",
    "    def by_who(name):\n",
    "        print('%s say: %s' % (name, word))\n",
    "    return by_who\n",
    "\n",
    "say_f = say('ok')\n",
    "say_f('小强')\n",
    "\n",
    "def fun8():\n",
    "    res = []\n",
    "    def put(x):\n",
    "        res.append(x)\n",
    "    def get():\n",
    "        return res\n",
    "    return put, get\n",
    "\n",
    "pt, gt = fun8()\n",
    "pt(11)\n",
    "pt(12)\n",
    "print('current values: %s' % gt())\n",
    "\n",
    "# fun8闭包类似于创建了一个隐藏的对象, 仅提供对象的方法用于操作对象的状态, 但是不能直接访问到对象或对象的状态; "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6059e67f-da89-4aeb-9222-0859e78ac959",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n"
     ]
    }
   ],
   "source": [
    "# 匿名函数 lambda\n",
    "f = lambda x, y : x + y\n",
    "print(f(5, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "163d1256-630c-46c0-a372-c34898eab0ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'generator'>\n",
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 关键字 yield: 返回函数执行时的中间结果, 但是函数并没有执行结束,  \n",
    "def func(n):\n",
    "    i = 0\n",
    "    while i < n:\n",
    "        yield i\n",
    "        i += 1 \n",
    "\n",
    "f = func(5)\n",
    "print(type(f))\n",
    "# 返回类型是 generator, 每个元素只能被访问一次, 可以迭代访问或转换成列表\n",
    "# 相对于直接处理中间结果, 可以返回给外部更自由处理\n",
    "\n",
    "#for x in f:\n",
    "#   print(x)\n",
    "\n",
    "print(list(f))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f4b65c26-1399-4f53-82bc-ac8b9c64a9a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 t [1, 2, 3] [1, 2] [5, 'a', 't'] [3, 'a'] ['a', 't']\n",
      "2187693097920\n",
      "5 Ij\n",
      "3\n",
      "1\n",
      "2187693097920 [8, 6, 5, 2]\n",
      "2187693097920 [8, 6, 5]\n",
      "2187693464832 [8, 6, 5, 2]\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# list []\n",
    "s = [1, 2, 3, 5, 'a', 't']\n",
    "print(s[0], s[-1], s[0:-3],  s[:2], s[3:], s[2::2], s[4::])  # 正序0开始, 倒序-1开始, 子集左闭右开, 第一个冒号前后有省略表示包括端点, 第二个冒号后表步进\n",
    "print(id(s))  # id获取内存地址\n",
    "s.append('dd')  # 追加\n",
    "s.extend(['ll','Ij'])  # 扩展\n",
    "s.insert(3, '跑了')  # 插入\n",
    "s.remove(2)  # 移除, 元素不存在时抛异常\n",
    "print(s.pop(3), s.pop())   # 弹出索引位的值, 默认最后一个元素\n",
    "print(s.index('a'))  # 查找并返回第一个索引\n",
    "print(s.count('ll'))  # 统计元素个数\n",
    "s.reverse()  # 倒序\n",
    "s.clear()  # 清空\n",
    "s.extend([8,2,6,5])\n",
    "s.sort(reverse=True)  # 默认正序排列\n",
    "print(id(s), s)  # 验证以上方法执行后, 实际操作的是原列表, 列表地址未变化\n",
    "\n",
    "l = s.copy()\n",
    "s.remove(2)  # 验证元素操作互不影响\n",
    "\n",
    "print(id(s), s)\n",
    "print(id(l), l)\n",
    "\n",
    "print(6 in l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0d97c0a5-a8a8-4ca4-bdfd-00539cb83326",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3) (4, 5, 6) 7 8 11 12\n",
      "1 0\n"
     ]
    }
   ],
   "source": [
    "# tuple ()\n",
    "t1 = (1, 2, 3)\n",
    "t2 = 4, 5, 6\n",
    "t3, t4 = 7, 8  # 元组解析需要对应个数的变量名\n",
    "t5, t6 = (11, 12)\n",
    "print(t1, t2, t3, t4, t5, t6)\n",
    "print(t1.count(2), t2.index(4))  # 仅提供了两个和list效果一样的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dc5f9467-fe73-42b2-a75b-431c1e684741",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 5, 8} {8, 3, 6}\n",
      "{0, 1, 2, 5}\n",
      "{0, 1, 2, 3, 5, 6, 8}\n",
      "{8}\n",
      "{0, 1, 2, 3, 5, 6}\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# set {}\n",
    "s = {3, 5, 7, 9}  # 数据不可重复\n",
    "s1 = set((5,5,8,2,1,1,0))  # 用于对元组去重\n",
    "s2 = set([3,3,6,6,6,8])  # 用于对列表去重\n",
    "print(s1, s2)\n",
    "print(s1 - s2)  # 差集\n",
    "print(s1 | s2)  # 并集\n",
    "print(s1 & s2)  # 交集\n",
    "print(s1 ^ s2)  # 并集中排除交集\n",
    "\n",
    "print(8 in s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a60dd250-1ced-48e2-99b8-036498206d12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 110, 'pk': 154, 'level': 'WARN'} {'a': 330, 'b': 115, 'c': 'wokk'} {'when': '2024', 'who': 'iqy'} {'name': 17, 'age': 66}\n",
      "154 WARN\n",
      "{} {1: 110, 'pk': 154, 'level': 'WARN'} dict_keys([1, 'pk', 'level']) dict_values([110, 154, 'WARN']) dict_items([(1, 110), ('pk', 154), ('level', 'WARN')])\n",
      "{'b': 115} 330 ('c', 'wokk')\n",
      "{'when': 2012, 'who': '韩立', 'type': '国漫', '题材': '修仙', '看点': '运镜'}\n"
     ]
    }
   ],
   "source": [
    "# dict\n",
    "d1 = {1: 110, 'pk': 154, 'level': 'WARN'}  # key唯一\n",
    "d2 = dict(a=330, b=115, c='wokk')   # 使用键值对参数构造\n",
    "d3 = dict([['when', '2024'], ['who', 'iqy']])  # 从二维列表构造\n",
    "d4 = dict([('name', 17), ('age', 66)])  # 从元组列表构造\n",
    "print(d1, d2, d3, d4)\n",
    "print(d1.get('pk'), d1['level'])  # 通过键取值, 由于无序, 无法通过索引取值\n",
    "\n",
    "d4 = d1.copy()\n",
    "d1.clear()\n",
    "print(d1, d4, d4.keys(), d4.values(), d4.items())  # keys获取键列表, values获取值列表, items获取键值二元组列表\n",
    "\n",
    "x = d2.pop('a')  # 弹出值\n",
    "y = d2.popitem()  # 弹出键值二元组\n",
    "print(d2, x, y)\n",
    "\n",
    "d3.update(when=2012) \n",
    "d3.update({'who': '韩立'})\n",
    "d3.update([('type', '国漫')])\n",
    "d3.update([['题材', '修仙']])  # 键值对, 词典, 二元组列表, 二维列表 可用坐参数更新词典\n",
    "d3['看点'] = '运镜'\n",
    "print(d3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "580fea8d-0272-479c-ade7-713f37c5bef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 3, 4, 5, 6]\n",
      "[2, 3, 4, 5, 6, 2, 3, 4, 5, 6]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6]\n",
      "('a', 15, 1, 'db')\n",
      "('f', 0, 'f', 0, 'f', 0)\n",
      "9\n",
      "2\n",
      "5\n",
      "30\n",
      "<class 'dict'> <class 'set'>\n",
      "{4}\n",
      "{8, 9, 4}\n",
      "{4}\n",
      "{9, 6}\n",
      "{5, 'p', 7}\n",
      "{9, 5, 6, 7}\n",
      "True\n",
      "True\n",
      "True\n",
      "{5, 6, 7, 22, 9}\n"
     ]
    }
   ],
   "source": [
    "# 其他集合操作\n",
    "l1 = [i for i in range(1, 11)]\n",
    "l2 = list(range(2, 7))\n",
    "print(l1, l2)\n",
    "\n",
    "print(l2 * 2)\n",
    "print(l1 + l2)\n",
    "print(('a', 15) + (1, 'db'))\n",
    "print(('f', 0) * 3)\n",
    "\n",
    "# list, tuple, str 可以使用 + *\n",
    "# set dic 不能使用 + *\n",
    "# list, tuple, set 都可使用 len(), min(), max(), sum()\n",
    "\n",
    "print(max({5, 8, 9}))\n",
    "print(len(['v', 'g']))\n",
    "print(min((88, 5)))\n",
    "print(sum((9, 5, 7, 1, 5, 3)))\n",
    "\n",
    "# {} 创建空字典, set() 创建空集合\n",
    "print(type({}), type(set())) \n",
    "s = {4, 8, 9}\n",
    "print(s.difference({'p'}, {8, 9}))  # 与多个集合的差集, 产生新的集合\n",
    "print(s)\n",
    "s.difference_update({'p'}, {8, 9})  # 移除与其他集合的交集 在原集合上操作\n",
    "print(s)\n",
    "\n",
    "s1 = {5, 9, 7, 6}\n",
    "print(s1.intersection({9, 6, 'p'}, {5, 9, 6}))  # 求多个集合的交集, intersection_update更新原集合\n",
    "print(s1.symmetric_difference({9, 6, 'p'}))  # 求并集中排除差集, symmetric_difference_update 更新原集合\n",
    "\n",
    "print(s1)\n",
    "print(s1.isdisjoint({8, 0}))  # 没有交集True, 有交集False\n",
    "print(s1.issubset({5, 9, 7, 6, 88}))  # 是另一集合的子集True\n",
    "print(s1.issuperset({9, 7}))  # 是另一集合的父集\n",
    "print(s1.union({22}))  # 并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "099ac701-3099-40ef-ba07-64fcc7dfa701",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'map'> [101, 102, 103] [<map object at 0x000001FD5CABDFF0>]\n",
      "['l to 1', 'k to 2', 'j to 3']\n",
      "<class 'filter'> [2, 3] <filter object at 0x000001FD5CAD33D0>\n",
      "<class 'int'> 6\n"
     ]
    }
   ],
   "source": [
    "# map(), filter(), reduce()\n",
    "li = [1, 2, 3]\n",
    "ml = map(lambda x: x+100, li)\n",
    "print(type(ml), list(ml), [ml])  # 使用list(M)获取map遍历操作结果\n",
    "\n",
    "l1 = ['l', 'k', 'j']\n",
    "m1 = map(lambda a,b: '%s to %d' % (b, a), li, l1)\n",
    "print(list(m1))\n",
    "\n",
    "f1 = filter(lambda x: x>1, li)\n",
    "print(type(f1), list(f1), f1)\n",
    "\n",
    "from functools import reduce\n",
    "r1 = reduce(lambda x,y : x + y, li)\n",
    "print(type(r1), r1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e68e6a60-7b12-426c-9655-a9dee1d3ae78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "公有方法  共有变量 ('私有方法 ', '私有变量')\n",
      "共有变量\n",
      "调用故障:  'PubAnPri' object has no attribute '__pri_fun'\n"
     ]
    }
   ],
   "source": [
    "# 类, 实例, 构造函数\n",
    "# 私有方法\n",
    "class PubAnPri:\n",
    "    pub = '共有变量'\n",
    "    __pri = '私有变量'\n",
    "\n",
    "    def __init__(self):\n",
    "        self.__other_pri = '其他私有'\n",
    "\n",
    "    def pub_fun(self):\n",
    "        print('公有方法 ', self.pub, self.__pri_fun()) \n",
    "\n",
    "    def __pri_fun(self):\n",
    "        return '私有方法 ', self.__pri\n",
    "\n",
    "p = PubAnPri()\n",
    "p.pub_fun()\n",
    "print(p.pub)\n",
    "try:\n",
    "    p.__pri_fun()\n",
    "except Exception as exp:\n",
    "    print('调用故障: ', exp)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d8f6383e-3ced-45d4-b38a-726c5d5d343a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"i'm\", 'not', 'like', 'python&java,', 'jus%t', 'for', 'money', 'a.n\\\\rd', 'you']\n",
      "c:/use/taj\n",
      "True True\n",
      "i'm not like scala&java\n",
      "like C#&ja\n",
      "i'm not like python&java\n"
     ]
    }
   ],
   "source": [
    "# 其他内置方法\n",
    "\n",
    "#import math\n",
    "#dir(math)  # 列举 modules 方法\n",
    "#help(math)  # modules 帮助文档\n",
    "\n",
    "ord('c')  # 取字符的ASCII 十进制码\n",
    "chr(99)  # ASCII码转字符\n",
    "\n",
    "s = \"i'm not like python&java, jus%t for money\\na.n\\\\rd\\tyou\"\n",
    "print(s.split())  # 默认按照所有分隔符分割: 空格, \\n, \\t\n",
    "print('/'.join(['c:', 'use', 'taj']))\n",
    "print('hongqi'.startswith('ho'), 'changj'.endswith('gj'))\n",
    "\n",
    "# 正则 re.sub(被替词, 替换词, 替换域, flags=re.IGNORECASE) 替换时忽略大小写\n",
    "import re\n",
    "\n",
    "s1 = \"i'm not like python&java\"\n",
    "print(re.sub('python', 'scala', s1))\n",
    "print(re.sub('pythoN', 'C#', s1[8:-2], flags=re.IGNORECASE))\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "474ff3ff-f8cc-4679-af26-b1bd213a9dd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.30000000000000004\n"
     ]
    }
   ],
   "source": [
    "# 类名 <- 首字母大写式驼峰: CameraBand\n",
    "# 包名, 模块名, 局部变量名, 函数名 <- 全小写 + 下划线式驼峰: get_property\n",
    "# 全局变量名 <- 全大写 + 下划线式驼峰: ENV\n",
    "# _模块私有变量\n",
    "# __类私有变量\n",
    "\n",
    "l = [0.2, 0.2, -0.1]\n",
    "# print(sum(l))\n",
    "\n",
    "from math import fsum\n",
    "print(fsum(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd0f565e-558d-4be6-8853-c3a7b7a7df76",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  },
  "panel-cell-order": [
   "92f4bd81-b18e-4273-86af-7339c2aa3740"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
